package net.hserver.apm.controller;

import net.hserver.apm.DataTask;
import net.hserver.apm.service.*;
import cn.hserver.core.interfaces.HttpRequest;
import cn.hserver.core.ioc.annotation.Autowired;
import cn.hserver.core.ioc.annotation.Controller;
import cn.hserver.core.ioc.annotation.GET;
import cn.hserver.core.server.util.JsonResult;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hxm
 */
@Controller(value = "/api/", name = "API")
public class ApiController {

    @Autowired
    private ServerInfoService serverInfoService;

    @Autowired
    private RequestInfoService requestInfoService;

    @Autowired
    private LogInfoService logInfoService;

    @Autowired
    private LinkInfoService linkInfoService;


    @Autowired
    private SqlInfoService sqlInfoService ;

    @Autowired
    private HomeService homeService;

    /**
     * 性能报告
     */
    @GET("serverInfo")
    public JsonResult serverInfo(HttpRequest request) {
        return JsonResult.ok().put("data", serverInfoService.getServerInfo());
    }

    /**
     * 请求报告
     */
    @GET("requestInfo")
    public JsonResult requestInfo(Integer page, Integer pageSize, String appName, String method, String chainId, String statusCode,String uri) {
        return JsonResult.ok().put("data", requestInfoService.getPage(page, pageSize, appName, method, chainId, statusCode,uri));
    }

    /**
     * 日志报告
     */
    @GET("logInfo")
    public JsonResult logInfo(int page, int pageSize, String appName, String level, String chainId) {
        return JsonResult.ok().put("data", logInfoService.getPage(page, pageSize, appName, level, chainId));
    }


    @GET("linkStack")
    public JsonResult linkStack(String chainId) {
        return JsonResult.ok().put("data", linkInfoService.getStack(chainId));
    }


    @GET("linkStackJson")
    public JsonResult linkStackJson(String chainId) {
        return JsonResult.ok().put("data", linkInfoService.getStackChat(chainId));
    }

    @GET("taskInfo")
    public JsonResult taskInfo() {
        Map<String, Object> data = new HashMap<>();
        data.put("day", DataTask.day);
        data.put("time", DataTask.runTime);
        data.put("switchBoolean", DataTask.switchBoolean);
        return JsonResult.ok().put("data", data);
    }

    @GET("setTaskInfo")
    public JsonResult setSaskInfo(Boolean switchBoolean, Integer day) {
        DataTask.switchBoolean = switchBoolean;
        DataTask.day = day;
        return JsonResult.ok();
    }

    @GET("getSqlInfo")
    public JsonResult getSqlInfo(String chainId) {
        return JsonResult.ok().put("data", sqlInfoService.getSqlInfo(chainId));
    }

    @GET("info")
    public JsonResult info() {
        return JsonResult.ok().put("data", homeService.getStats());
    }

}
